package com.ylxl.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ylxl.common.annotation.Log;
import com.ylxl.common.enums.BusinessType;
import com.ylxl.common.result.AjaxResult;
import com.ylxl.common.result.BaseController;
import com.ylxl.common.utils.StringUtils;
import com.ylxl.entity.User;
import com.ylxl.entity.request.*;
import com.ylxl.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户管理
 * @Auther Gilbert
 * @Date 2023/5/30 19:51
 */
@Slf4j
@RestController
@RequestMapping("/user")
@Api(tags = "用户管理", hidden = true)
public class UserController extends BaseController {
    @Autowired
    private ISysUserService userService;


    /**
     * 分页查询用户列表信息
     * @param queryUser
     * @return
     */
    @Log(title = "分页查询用户列表信息", businessType = BusinessType.SELECT)
    @PostMapping("/queryUserList")
    @ApiOperation(value = "分页查询用户列表信息", response = AjaxResult.class)
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = User.class),})
    public AjaxResult queryUserList(@RequestBody QueryUser queryUser) {
        AjaxResult ajax = AjaxResult.success();
        try {
            //进行分页操作
            PageInfo<User> pageInfo = new PageInfo<>();
            if (StringUtils.isNotEmpty(queryUser.getPageNum()) && StringUtils.isNotEmpty(queryUser.getPageSize())) {
                PageHelper.startPage(queryUser.getPageNum(), queryUser.getPageSize());
            }
            List<User> users = userService.selectUserList(queryUser);
            pageInfo = new PageInfo<>(users);
            ajax.put(AjaxResult.DATA_TAG, users);
            ajax.put(AjaxResult.TOTAL, pageInfo.getTotal());
        } catch (Exception e) {
            log.error("查询用户列表日志信息，异常信息：{}", e);
            return toAjax(false);
        }
        return ajax;
    }

    /**
     * 用户管理保存
     * @param user
     * @return
     */
    @Log(title = "用户管理保存", businessType = BusinessType.INSERT)
    @PostMapping("/insert")
    @ApiOperation(value = "用户管理保存", response = AjaxResult.class)
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = Integer.class),})
    public AjaxResult add(@RequestBody UserCmdOrMdy user){
        if (userService.checkUserNameUnique(user)){
            return error("新增用户'" + user.getUserName() + "'失败，登录账号已存在");
        }
        return toAjax(userService.insertUser(user));
    }

    /**
     * 用户管理编辑
     * @param user
     * @return
     */
    @Log(title = "用户管理编辑", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation(value = "用户管理编辑", response = AjaxResult.class)
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = Integer.class),})
    public AjaxResult update(@RequestBody UserCmdOrMdy user){
        if (userService.checkUserNameUnique(user)){
            return error("编辑用户'" + user.getUserName() + "'失败，登录账号已存在");
        }
        return toAjax(userService.updateUser(user));
    }
    /**
     * 用户管理根据id查询明细
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "用户管理根据id查询明细", response = AjaxResult.class)
    @Log(title = "用户管理根据id查询明细", businessType = BusinessType.SELECT)
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = User.class),})
    public AjaxResult getInfo(@PathVariable(value = "id", required = true) Integer id) {
        return AjaxResult.success(userService.selctById(id));
    }
    /**
     * 用户管理根据ids集合删除
     */
    @PostMapping("/remove")
    @ApiOperation(value = "用户管理根据ids集合删除", response = AjaxResult.class)
    @Log(title = "园区管理根据ids集合删除", businessType = BusinessType.DELETE)
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = Integer.class),})
    public AjaxResult remove(@RequestBody DeleteRequest deleteRequest) {
        return AjaxResult.success(userService.deleteByIds(deleteRequest.getIds()));
    }
    /**
     * 用户管理启用或者禁用
     */
    @PostMapping("/enableOrDisable")
    @ApiOperation(value = "用户管理启用或者禁用", response = AjaxResult.class)
    @Log(title = "用户管理启用或者禁用", businessType = BusinessType.DELETE)
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = Integer.class),})
    public AjaxResult enableOrDisable(@RequestBody UserStatusRequest userStatusRequest) {
        return AjaxResult.success(userService.enableOrDisable(userStatusRequest));
    }/**
     * 修改密码
     */
    @PostMapping("/changePassword")
    @ApiOperation(value = "修改密码", response = AjaxResult.class)
    @Log(title = "修改密码", businessType = BusinessType.DELETE)
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = Integer.class),})
    public AjaxResult changePassword(@RequestBody UserPasswordMdy userPasswordMdy) {
        return AjaxResult.success(userService.changePassword(userPasswordMdy));
    }
}

